package com.demo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.module.commonui.R;
import com.tange.base.toolkit.StringUtils;
import com.tg.appcommon.android.ResourcesUtil;

public class SettingItemSwitchViewEx extends SettingItemSwitchView {
    private String mMark;
    private int mMarkTextSize = 10;
    private boolean mShowLeftImage;
    private TextView mTvMark;
    private ImageView mLeftImage;
    private boolean isDeleteMode = false;
    private ImageView mCheckImage;
    private boolean mCheck = false;
    public SettingItemSwitchViewEx(Context context) {
        super(context);
        initContentView();
    }

    public void setDeleteMode(boolean isDeleteMode){
        this.isDeleteMode = isDeleteMode;
        if (mCheckImage != null){
            mSwitch.setVisibility(this.isDeleteMode? GONE : View.VISIBLE);
            mCheckImage.setVisibility(this.isDeleteMode? VISIBLE : View.GONE);
        }
    }

    public SettingItemSwitchViewEx(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SettingItemSwitchViewEx(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initParam(context, attrs);
        initContentView();
    }

    private void initParam(Context context, @Nullable AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SettingItemSwitchViewEx);
        mMark = typedArray.getString(R.styleable.SettingItemSwitchViewEx_switch_text_mark);
        mShowLeftImage = typedArray.getBoolean(R.styleable.SettingItemSwitchViewEx_switch_left_image_visibility, false);
        mMarkTextSize = typedArray.getInt(R.styleable.SettingItemSwitchViewEx_switch_text_mark_size, mMarkTextSize);
        typedArray.recycle();
    }

    @Override
    public int getLayoutResID() {
        return R.layout.settings_item_switch_ex;
    }

    public void setCheckImageOnClickListener(@Nullable OnClickListener l) {
        if (mCheckImage != null){
            mCheckImage.setOnClickListener(l);
        }
    }

    public void changeImageState(){
        if (this.isDeleteMode){
            this.mCheck =  !this.mCheck;
            if (this.mCheck){
                mCheckImage.setImageDrawable(ResourcesUtil.getDrawable(R.drawable.ic_tange_global_icon_radio_btn_yes));
            }else{
                mCheckImage.setImageDrawable(ResourcesUtil.getDrawable(R.drawable.ic_tange_global_icon_radio_btn_no));
            }
        }
    }

    public void setImageCheckState(boolean isCheck){
        if (this.isDeleteMode){
            this.mCheck = isCheck;
            if (this.mCheck){
                mCheckImage.setImageDrawable(ResourcesUtil.getDrawable(R.drawable.ic_tange_global_icon_radio_btn_yes));
            }else{
                mCheckImage.setImageDrawable(ResourcesUtil.getDrawable(R.drawable.ic_tange_global_icon_radio_btn_no));
            }
        }
    }

    public boolean getCheckImageState(){
        return this.mCheck;
    }

    private void initContentView() {
        mTvMark = mView.findViewById(R.id.tv_item_text_mark);
        mCheckImage = mView.findViewById(R.id.check_imageButton);
        if (mTvMark != null){
            mTvMark.setText(mMark);
            mTvMark.setTextSize(mMarkTextSize);
        }

        mLeftImage = mView.findViewById(R.id.left_arrow);
        if (mLeftImage != null){
            mLeftImage.setVisibility(mShowLeftImage? VISIBLE : GONE);
        }
    }

    public ImageView getLeftImage(){
        return mLeftImage;
    }
    public void setMark(String mark){
        mMark = mark;
        mTvMark = mView.findViewById(R.id.tv_item_text_mark);
        if (mTvMark != null){
            mTvMark.setText(mMark);
            if (StringUtils.isEmpty(mMark)){
                mTvMark.setVisibility(GONE);
            }
        }
    }

}
